//
//  ActionSheetViewController.m
//  StaffTax
//
//  Created by 侯伟 on 2020/1/3.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "ActionSheetViewController.h"

@interface ActionSheetViewController ()

@property (nonatomic,strong)UIButton* dismissButton;

@end

@implementation ActionSheetViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColor.clearColor;
    
    self.dismissButton = [[UIButton alloc] initWithFrame:self.view.bounds];
    [self.dismissButton addTarget:self action:@selector(onDismiss:) forControlEvents:UIControlEventTouchUpInside];

    self.dismissButton.frame = self.view.bounds;
    self.dismissButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view insertSubview:_dismissButton atIndex:0];
    
    // Do any additional setup after loading the view.
}

- (void)presentOverViewController:(UIViewController*)viewController{
    
    [viewController.view endEditing:YES];
    
    [self presentOverViewController:viewController compplete:NULL];
}

/// 显示
/// @param viewController  显示在那个控制器之上
- (void)presentOverViewController:(UIViewController*)viewController compplete:(void (^)(void))complete{
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        
    __weak UIView* weakView = self.view;
    __weak UIView* weakPopView = self.popView;
    
    weakPopView.alpha = 0;
    
    [Queue asyncMain:^{
        [viewController presentViewController:self animated:NO completion:^{
           
            CGRect originFrame = weakPopView.frame;
            weakPopView.frame = CGRectMake(0, weakView.bounds.size.height, originFrame.size.width, originFrame.size.height);
            [UIView animateWithDuration:0.25 animations:^{
                weakView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0x66/255.0];
                weakPopView.alpha = 1;
                weakPopView.frame = originFrame;
                if(complete){
                    complete();
                }
            }];
        }];
    }];
}


- (void)dismissActionSheetController:(void (^)(void))completion{
    
    [UIView transitionWithView:self.popView duration:0.25 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        self.popView.frame = CGRectMake(0, self.view.bounds.size.height,self.popView.frame.size.width, self.popView.frame.size.height);
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:completion];
        
    }];
}

- (void)onDismiss:(UIButton*)sender{
    
    [self dismissActionSheetController:NULL];
    if (self.cancelBlockClick) {
        self.cancelBlockClick();
    }
}

@end
